home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / comm / mail / YamNet.lha / rexxtra12.lha / rexx / Count.rexx < prev    next >
OS/2 REXX Batch file  |  1990-03-18  |  3KB  |  109 lines

  1. /* count.rexx */
  2.  
  3. /*
  4.      Format
  5.  
  6.        COUNT [[FILE] <file>] [W|WORDS] [L|LINES] [C|CHARS] [LO|LONGEST]
  7.  
  8.    Count the characters, words, lines and/or longest line in a file
  9.    or STDIN, if no file supplied.
  10.  
  11. */
  12.  
  13. signal on break_c
  14. call addlib 'rexxextra.library',-20,-30,0
  15.  
  16. facility = 'Count'
  17. retcode = 0
  18. template = 'W=WORDS/S,L=LINES/S,C=CHARS/S,LO=LONGEST/S,P=PAGE/S,S=SIZE/K,FILE'
  19. dtemplate = 'FILE,W=WORDS/S,L=LINES/S,C=CHARS/S,LO=LONGEST/S,P=PAGE,S=SIZE/K'
  20. args. = ''
  21.  
  22. parse arg g_c
  23. do while g_c='?'
  24.   options prompt dtemplate': '  /* this template is      */
  25.   parse pull g_c        /* displayed to the user */
  26.   if g_c='?' then do
  27.     g_s=sourceline(3)
  28.     if pos('/*',g_s)=0 then break; if pos('*/',g_s)>0 then break
  29.     say
  30.     g_s=sourceline(4)
  31.     do i=5 while pos('*/',g_s)=0; say g_s; g_s=sourceline(i); end
  32.     say
  33.     end
  34.   end
  35. interpret Cparse(g_c,template,'args')
  36. if args.ERRCODE > 1 then do; say facility'-E-BADARGS,' args.ERRTEXT; exit 5; end
  37.  
  38. aa = 'C W L LO P'
  39. parse value aa 'STDIN 66' with C W L LO P tfile pl . putecom
  40. saycom = "say '"
  41. cmd.    = ''
  42. cmd.C    = 'chars=chars+LL;'
  43. cmd.W    = 'wrds=wrds+words(rec);'
  44. cmd.L    = 'lns=lns+1;'
  45. cmd.LO    = 'if LL>longest then longest=LL;'
  46. cmd.P    = 'ln=ln+1;if ln>pl then do;ln=0;pg=pg+1;end;'
  47. asdf.    = ''
  48. asdf.C    = "Chars:' chars', "
  49. asdf.W    = "Words:' wrds', "
  50. asdf.L    = "Lines:' lns', "
  51. asdf.LO = "Longest:' longest', "
  52. asdf.P    = "Pages:' pg', "
  53.  
  54. if args.W+args.L+args.C+args.LO+args.P = 0 then do
  55.   args.W=1;args.L=1;args.C=1;args.LO=1;args.P=1
  56.   end
  57. if args.S ~= '' then do
  58.   if ~datatype(args.S,'N') then call sayerror('SIZE value must be numeric')
  59.   pl = args.S
  60.   asdf.P = "Pages:' pg '('pl'/page)'"
  61.   end
  62. if args.FILE ~= '' then do
  63.   if ~exists(args.FILE) then call sayerror('File' args.FILE 'not found')
  64.   if ~open('COUNTHANDLE',args.FILE,'R') then
  65.     call sayerror('Could not open' args.FILE 'for input')
  66.   tfile = 'COUNTHANDLE'
  67.   saycom = saycom"File:' args.FILE; say '"
  68.   end
  69. do jj = 1 to words(aa)
  70.   kk = word(aa,jj)
  71.   if args.kk then do
  72.     putecom = putecom || cmd.kk
  73.     saycom = saycom || asdf.kk
  74.     end
  75.   end
  76. if right(saycom,2) = ', ' then
  77.   saycom = left(saycom,length(saycom)-3)
  78.  
  79. chars = 0; lns = 0; wrds = 0; longest = 0; ln = 0; pg = 1
  80. rec = readln(tfile)
  81. do until eof(tfile)
  82.   LL = length(rec)
  83.   interpret putecom
  84.   rec = readln(tfile)
  85. end /* do */
  86. interpret saycom
  87.  
  88. GetOut:
  89. exit retcode
  90.  
  91. sayerror:
  92.   parse arg x
  93.   say x
  94.   exit 10
  95.  
  96. break_c:
  97. break_d:
  98. break_e:
  99. break_f:
  100.   say facility'-E-BREAK, Control-C interrupt'
  101.   exit 20
  102. failure:
  103.   say facility'-E-FAIL, Line:' sigl', Error:' rc; retcode = rc; signal GetOut
  104. syntax:
  105.   say facility'-E-SYNTAX, Line:' sigl', Error:' rc; retcode = rc; signal GetOut
  106. error:
  107.   say facility'-E-ERROR, Line:' sigl', Error:' rc; retcode = rc; signal GetOut
  108.  
  109.